/**
 * 
 */
package org.t2.backstage.module.recharge;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.banish.sql.core.util.QuerySet;
import org.gaming.backstage.PageData;
import org.gaming.service.apidoc.annotation.ApiDocument;
import org.gaming.tool.ConsoleUtil;
import org.gaming.tool.DateTimeUtil;
import org.gaming.tool.GsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.t2.backstage.module.gameserver.model.GameServer;
import org.t2.backstage.module.gameserver.service.GameServerService;
import org.t2.backstage.module.player.query.PlayerCommonQuery;
import org.t2.backstage.module.recharge.model.RechargeProductCfg;
import org.t2.backstage.module.recharge.service.RechargeCallbackService;
import org.t2.backstage.module.recharge.service.RechargeProductCfgService;
import org.t2.backstage.module.recharge.vo.RechargeSummaryVo;

import jakarta.annotation.PostConstruct;

/**
 * 
 */
@Controller
public class RechargeSummaryController {
	
	@Autowired
	private RechargeCallbackService rechargeCallbackService;
	@Autowired
	private GameServerService gameServerService;
	@Autowired
	private RechargeProductCfgService rechargeProductCfgService;
	
	
	
	@ApiDocument("请求充值详情统计数据")
	@RequestMapping(value = "/rechargeSummary/data.auth")
	@ResponseBody
	public PageData<RechargeSummaryVo> data(PlayerCommonQuery query) {
		QuerySet querySet = new QuerySet();
		//渠道选择
		if(query.getChannels() != null && !query.getChannels().isEmpty()) {
			querySet.findInSet("channel_id", query.getChannels());
		}
		//服务器选择
		if(query.getServerUids() != null && !query.getServerUids().isEmpty()) {
			Set<Integer> serverIds = new HashSet<>();
			for(int serverUid : query.getServerUids()) {
				GameServer gameServer = gameServerService.getEntity(0, serverUid);
				if(gameServer == null) {
					continue;
				}
				serverIds.add(gameServer.getServerId());
			}
			querySet.findInSet("server_id", serverIds);
		}
		//开始时间
		long startTime = query.getStartTime();
		if(startTime > 0) {
			String startBorn = DateTimeUtil.formatMillis(DateTimeUtil.YYYYMMDD, startTime * 1000L);
			querySet.addCondition("insert_time >= ?", startBorn);
		}
		//结束时间
		long endTime = query.getEndTime();
		if(endTime > 0) {
			String endBorn = DateTimeUtil.formatMillis(DateTimeUtil.YYYYMMDD, endTime * 1000L);
			querySet.addCondition("insert_time <= ?", endBorn);
		}
		querySet.addCondition("give = 1");
		querySet.groupBy("group by product_id");
		querySet.formWhere();
		
		String sql = "select product_id AS productId,count(1) AS buyNum, sum(money) AS totalPay from recharge_callback ";
		sql += querySet.getWhere();
		
		List<RechargeSummaryVo> voList = rechargeCallbackService.queryAliasObject(RechargeSummaryVo.class, sql, querySet.getParams());
		long allPay = 0;
		int allBuyNum = 0;
		for(RechargeSummaryVo resultVo : voList) {
			RechargeProductCfg rechargeCfg = rechargeProductCfgService.getEntity(0, resultVo.getProductId());
			if(rechargeCfg != null) {
				resultVo.setProductName(rechargeCfg.getName());
				resultVo.setProductType(rechargeCfg.getType());
				resultVo.setPrice(rechargeCfg.getPrice());
			}
			
			allPay += resultVo.getTotalPay();
			allBuyNum += resultVo.getBuyNum();
			//数据统计中的单位是分，但是显示的单位是元
			resultVo.setTotalPay(resultVo.getTotalPay() / 100);
		}
		for(RechargeSummaryVo resultVo : voList) {
			if(allPay > 0) {
				float floatRate = resultVo.getTotalPay() * 1.0f / allPay * 100;
				BigDecimal payRate = new BigDecimal("" + floatRate).setScale(2, RoundingMode.UP);
				resultVo.setPayRate(payRate.toString() + "%");
			}
			if(allBuyNum > 0) {
				float floatRate = resultVo.getBuyNum() * 1.0f / allBuyNum * 100;
				BigDecimal payRate = new BigDecimal("" + floatRate).setScale(2, RoundingMode.UP);
				resultVo.setNumRate(payRate.toString() + "%");
			}
		}
		
		PageData<RechargeSummaryVo> pageData = new PageData<>();
		pageData.setData(voList);
		pageData.setCount(voList.size());
		return pageData;
	}
	
	@PostConstruct
	private void console() {
		ConsoleUtil.addFunction("res", () -> {
			test();
		});
	}
	
	public void test() {
		PlayerCommonQuery query = new PlayerCommonQuery();
		PageData<RechargeSummaryVo> pageData = this.data(query);
		System.out.println("pageData.getData().size() " + pageData.getData().size());
		for(RechargeSummaryVo vo : pageData.getData()) {
			System.out.println(GsonUtil.toJson(vo));
		}
	}
}
